$(function () {
    var page = 1;
    var pageSize = 5;

    function render() {
        $.ajax({
            url: "/user/queryUser",
            type: "GET",
            data: {
                page: page,
                pageSize: pageSize
            },
            success: function (data) {
                console.log(data);
                var html = template("usertable", data);
                $("tbody").html(html);

                $("#paginator").bootstrapPaginator({
                    bootstrapMajorVersion: 3,
                    currentPage: page,
                    totalPages: Math.ceil(data.total / pageSize),
                    size: "small",

                    onPageClicked: function (a, b, c, index) {
                        page = index;

                        render();
                    }
                });
            }
        });
    }

    render();

    function updateuser(id, isDelete) {
        $.ajax({
            url: "/user/updateUser",
            type: "POST",
            data: {
                id: id,
                isDelete: isDelete
            },
            success: function (data) {
                //console.log(data);
                render();
            }
        });
    }

    //实现更新用户状态的功能
    $("tbody").on("click", "button", function () {
        //判断当前点击的是禁用，还是启用
        var id = $(this).data("id");
        // alert(id);

        //禁用
        if ($(this).hasClass("btn-danger")) {
            updateuser(id, 0);
        }

        //启用
        if ($(this).hasClass("btn-success")) {
            updateuser(id, 1);
        }
    });
});